﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using THO7AlgoritmTimerApplication.Filters;

namespace THO7AlgoritmTimerApplication
{
    public class Image
    {
        private Bitmap _source;
        private BitmapData _data;

        public BitmapData Data
        {
            get
            {
                return _data;
            }
            // set { _data = value; }
        }

        private int _width;
        private int _height;

        public Image(Bitmap source, bool newBitmap = false)
        {
            if (newBitmap)
            {
                
                _source = new Bitmap(source);
            }
            else
            {
                _source = source;
            }
            _width = source.Width;
            _height = source.Height;
        }

        public Image(int width, int height)
        {
            _source = new Bitmap(width, height);
            _width = width;
            _height = height;
        }

        public void Lock()
        {
            _data = _source.LockBits(new Rectangle(0, 0, _width, _height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        }

        public Bitmap ApplyFilter(Filter filt)
        {
            Lock();
            filt.InitFilter(_height, _width, _data, _source);
            Bitmap output = filt.RunFilter();
            Unlock();
            return output;
        }

        public void Unlock()
        {
            try {
            _source.UnlockBits(_data);
            }
            catch { ;}
        }

        public unsafe int compareTo(Image comparedTo)
        {
          /*  Lock();
            int afwijking = 255 / 10; //afwijking is ongeveer 10%
            int dif = 0;
            float _d = 0;
            int size = _data.Stride * _data.Height;
            byte* srcPnt = (byte*)_data.Scan0;
            comparedTo.Lock();

            byte* cmpPnt = (byte*)comparedTo.Data.Scan0;

            for (int i = 0; i < size; i++)
            {
                if (srcPnt[i] != cmpPnt[i])
                {
                    if (srcPnt[i] < cmpPnt[i] * 0.9 ||
                        srcPnt[i] > cmpPnt[i] * 1.1)
                        /* als hij dus buiten de 10% afwijking valt 
                         * dan wordt het pas als een echt verschil gerekend
                        *
                        dif++;
                }
            }
            Unlock();
            comparedTo.Unlock();
            Console.WriteLine(dif);
            Console.WriteLine(size);
            Console.WriteLine((dif / size) * 100);
            _d = (float)dif / (float)size;
            _d *= (float)100;
    */
            return 0;
        }
    }
}
